<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    :disabled="baTable.form.operate == 'view'"
                >
                    <FormItem
                        :label="t('line.PORTDIS')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.PORTDIS"
                        :input-attr="{
                    multiple: false,
                    pk: 'PORT',
                    field: 'PORT',
                    'remote-url': port + 'index',
                }"
                        placeholder="点击选择起运港"
                    />
                    <FormItem   v-if="false"  type="string" v-model="baTable.form.items!.PORTLOAD" prop="PORTLOAD" />
                    <FormItem   v-if="false"  type="string" v-model="baTable.form.items!.ids" prop="ids" />
                    <FormItem :label="t('line.LINE')" type="string" v-model="baTable.form.items!.LINE" prop="LINE" :input-attr="{ placeholder: t('Please input field', { field: t('line.LINE') }) }" />
                <FormItem :label="t('line.DISTANCE')" type="number" prop="DISTANCE" v-model.number="baTable.form.items!.DISTANCE" :input-attr="{ step: '1', placeholder: t('Please input field', { field: t('line.DISTANCE') }) }" />
                 <FormItem :label="t('line.REMARK')" type="textarea" v-model="baTable.form.items!.REMARK" prop="REMARK" :input-attr="{ placeholder: t('Please input field', { field: t('line.REMARK') }) }" />
                    <FormItem
                        :label="t('line.OP')"
                        type="string"
                        v-model="baTable.form.items!.OP"
                        prop="OP"
                        :input-attr="{ placeholder: '当前登录用户','disabled':true }"
                    />
                    <FormItem :label="t('line.OPTIME')"
                              type="date"
                              v-model="baTable.form.items!.OPTIME"
                              prop="OPTIME"
                              :input-attr="{ placeholder: '当前日期','disabled':true  }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer v-if="baTable.form.operate !== 'view'">
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { port } from '/@/api/controllerUrls'


const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    LINE: [buildValidatorData({name: 'required', title: t('line.LINE')})],
    OPTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('line.OPTIME') })})],
})

</script>

<style scoped lang="scss"></style>
